package com.bawei.fuxi.base.mvp;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;

public class BasePresenter<V>{
    private WeakReference<V> mVWeakReference;
    protected CompositeDisposable mCompositeDisposable = new CompositeDisposable();

    //绑定
    public void attach(V view){
        mVWeakReference = new WeakReference<>(view);
    }

    //解绑
    public void distach(){
        if(mVWeakReference != null){
            mVWeakReference.clear();
            mVWeakReference = null;
        }
        if(mCompositeDisposable != null){
            mCompositeDisposable.clear();
        }
    }

    //判断
    public boolean isAttach(){
        return mVWeakReference != null && mVWeakReference.get() != null;
    }

    //返回view
    public V getView(){
        return mVWeakReference != null && mVWeakReference.get() != null ? mVWeakReference.get() : null;
    }
}
